import { CartItem } from "@/type/cartTypes"
import { observable, action } from "mobx-miniprogram"
import userStore from "@/store/userStore"
import { Detail } from "@/type/detailTypes"

import { addCartOrCahngeGoodCountAPI, changeGoodAllCheckedAPI, changeGoodCheckedAPI, deleteCartAPI, fetchCartListAPI } from "@/api/cartAPI"

const cartStore = observable({
  cartList: <CartItem[]>[],
  /**
   * @description 加入购物车
   */
  addCart: action((goodsInfo: Detail, { count, goodsId, blessing }: { count: number, goodsId: number, blessing: string }) => {
    return new Promise(async (resolve, reject) => {
      if (userStore.token) {
        try {
          await addCartOrCahngeGoodCountAPI({ count, goodsId, blessing })
          resolve("加入成功")
        } catch (error) {
          reject("加入失败")
        }
      } else {

      }
    })
  }),
  /**
 * @description 获取购物车列表
 */
  fetchCartList: action(() => {
    return new Promise(async (resolve, reject) => {
      if (userStore.token) {
        try {
          const { data: { data } } = await fetchCartListAPI()
          cartStore.cartList = data
          resolve("获取列表成功")
        } catch (error) {
          reject("获取列表失败")
        }
      } else {

      }
    })
  }),
  /**
  * @description 删除购物车商品
  */
  deleteCart: action((goodsId: number) => {
    return new Promise(async (resolve, reject) => {
      if (userStore.token) {
        try {
          await deleteCartAPI(goodsId)
          cartStore.cartList = cartStore.cartList.filter(item => item.goodsId !== goodsId)
          resolve("商品删除成功")
        } catch (error) {
          reject("商品删除失败")
        }
      } else {

      }
    })
  }),
  /**
   *@description 勾选商品状态的回调函数 
   */
  changeGoodChecked: action((goodsId: number, isChecked: 0 | 1) => {
    return new Promise(async (resolve, reject) => {
      if (userStore.token) {
        try {
          await changeGoodCheckedAPI(goodsId, isChecked)
          /*const find: CartItem = cartStore.cartList.find(item => item.goodsId === goodsId) as CartItem
          find.isChecked=isChecked*/
          cartStore.fetchCartList()
          resolve("状态修改成功")
        } catch (error) {
          reject("状态修改失败")
        }
      } else {

      }
    })
  }),
  /**
 *
 * @description 全选和取消全选的回调函数
 */
  changeGoodAllChecked: action((isChecked: 0 | 1) => {
    return new Promise(async (resolve, reject) => {
      if (userStore.token) {
        try {
          await changeGoodAllCheckedAPI(isChecked)
          // cartStore.cartList.forEach((item: CartItem) => item.isChecked = isChecked)
          await cartStore.fetchCartList()
          resolve("状态修改成功")
        } catch (error) {
          reject("状态修改失败")
        }
      } else {

      }
    })
  }),
  /**
   * 
   * @description 是否全部为1
   */
  get isCheckedAll() {
    return this.cartList.every((item: CartItem) => item.isChecked === 1)
  },

  /**
 * 
 * @description 勾选商品总价
 */
  get totalPrice() {
    return this.cartList.filter((item: CartItem) => item.isChecked === 1).reduce((sum: number, item: CartItem) => sum+=item.price as number * item.count as number, 0)
  }
})

export default cartStore